<div class="page">
	<div class="menu">
		<?php include('modules/members/dashboard.php');?>
    </div>
	<div class="products-list" style="overflow:hidden;">
    <?php
    if($_GET['edit'] == true){
		$sql = 'SELECT * FROM stickers WHERE id='.$_GET['id'];
		$q = mysql_query($sql)or die();
		$rs= mysql_fetch_assoc($q);
	}
	?>
    <?php if($_GET['edit'] == true){ ?>
    	<input type="hidden" id="id" value="<?php echo $_GET['id']; ?>" />
    	<p style="float:right;" id="button">
        	<a href="#" class="btn" style="font-size:25px;cusor:pointer;" id="update_print"><span>Update</span></a>
            <span id="status" ></span>
       	</p>	 
    <?php }else{ ?>
        <p style="float:right;" id="button">
        	<a href="#" class="btn" style="font-size:25px;cusor:pointer;" id="print"><span>ต่อไป</span></a>
            <span id="status" ></span>
       	</p>	 
	<?php } ?>   
        <p style="float:right;" id="button">
        	<a href="" class="btn" style="font-size:25px;cusor:pointer;" id="upload"><span>เลือกรูปภาพ</span></a>
            <span id="status" ></span>
       	</p>   	    
        <ul id="files" style="float: left;margin-top: 80px;padding: 0px;width: 300px;">
        <?php
            $pic_num = isset($_SESSION['img'])? count($_SESSION['img']):0;
            if($pic_num != 0){               
               echo '<li style="float:left;list-style:none;"><img src="stickers/'.$_SESSION['img'].'" width="300" /><input type="hidden" name="img[]" value="'.$_SESSION['img'].'" /></li>';
            }elseif($_GET['edit'] == true){
				echo '<li style="float:left;list-style:none;"><img src="stickers/'.$rs['img'].'" width="300" /><input type="hidden" name="img[]" value="'.$rs['img'].'" /></li>';
				$_SESSION['img'] = $rs['img'];
			}else{
				echo '<img src="stickers/nopicture.gif" width="200"  />';
			}
        ?>
        </ul>    
        	<textarea name="jw" id="p_detail" cols="70" rows="15" style="margin-left:10px;">
				<?php isset($rs['txt']) ? print $rs['txt']: '' ;?>
           	</textarea>
    </div>
</div>
<script type="text/javascript">
$(function(){
	$('#p_detail').wysiwyg();
	var btnUpload=$('#upload');
	var status=$('#status');
	new AjaxUpload(btnUpload, {
		action: 'upload-file.php',
		name: 'uploadfile',
		onSubmit: function(file, ext){
			if (!(ext && /^(jpg|png|jpeg|gif)$/.test(ext))){ 
				// extension is not allowed 
				status.text('ไฟล์ที่คุณสามารถอัพโหลดได้คือ .jpg, .jpeg, .png และ .gif เท่านั้น');
				return false;
			}
			status.text('Uploading...');
		},
		onComplete: function(file, response){
			//On completion clear the status
			status.text('');
			//Add uploaded file to list
			var imgstatus = response.split(',');
			
			if(imgstatus[0]==="success"){
				$('#files').html('');
				$('<li style="float:left;list-style:none;"></li>').appendTo('#files').html('<img src="stickers/'+imgstatus[1]+'" width="300" /><input type="hidden" name="img[]" value="'+imgstatus[1]+'"/>').addClass('success');
				count_img++;
				
			} else{
				$('<li style="float:left;list-style:none;"></li>').appendTo('#files').text(imgstatus[1]).addClass('error');
			}
	
		}
	});
	
	$('#print').click(function(){
		$.ajax({
			type	: 'POST',
			url		: 'query.php',
			data	: {go_print:true, value:$('#p_detail').val()},
			dataType: 'html',
			success	: function(msg){
				location.href = "?m=stickers&p=print&id="+msg;
			}
		});
		return false;
	});
	
	$('#update_print').click(function(){
		$.ajax({
			type	: 'POST',
			url		: 'query.php',
			data	: {update_print:true, value:$('#p_detail').val(), id:$('#id').val()},
			dataType: 'html',
			success	: function(msg){
				location.href = "?m=stickers&p=view";
			}
		});
		return false;
	});
		
});
</script>
<style type="text/css">
.wysiwyg{
	float:right;
}
</style>